<template>
  <d2-container :filename="filename">
    <template slot="header">评论列表</template>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="listData"
      :pagination="pageData"
      add-title="我的新增"
      :form-options="formOptions"
      :rowHandle="rowHandle"
      @push-item="onPush"
      @del-item="onDel"
      @dialog-open="handleDialogOpen"
      @row-add="handleRowAdd"
      @dialog-cancel="handleDialogCancel">
      <!--<el-button slot="header" style="margin-bottom: 5px" @click="addRow">新增</el-button>-->
    </d2-crud>
  </d2-container>
</template>

<script>
// "update_time": 0,
// "course_id": 1,
// "chapter_id": 1,
// "sort_number": 0,
// "content": "吃饭了吗",
// "user_id": 1,
// "user_name": "梅振如",
// "user_avatar": "http://cdn.dianzedushu.com/user1.jpg",
// "like_sum": 0,
// "status": 0
import utils from '@/libs/util.gaed'
import settings from '@/setting'
const BASE_URL = '/study/comments'
export default {
  data () {
    return {
      listData: [],
      pageData: {},
      filename: __filename,
      columns: [
        {
          title: '关联课程ID',
          key: 'course_id'
        },
        {
          title: '内容',
          key: 'content'
        },
        {
          title: '点赞数',
          key: 'like_sum'
        },
        {
          title: '用户信息',
          key: 'userInfo'
        },
        {
          title: '更新时间',
          key: 'updateTimeStr'
        },
        {
          title: '状态',
          key: 'statusStr'
        }
      ],
      rowHandle: {
        width: '220px',
        custom: [
          {
            text: '删除',
            type: 'warning',
            size: 'small',
            emit: 'del-item'
          },
          {
            text: '置顶',
            size: 'small',
            emit: 'push-item'
          }
        ]
      },
      data: [],
      popTemplate: {},
      formOptions: {
        labelWidth: '80px',
        labelPosition: 'left',
        saveLoading: false
      }
    }
  },
  mounted () {
    this.initList()
  },
  methods: {
    initList () {
      utils.fetchData.call(this, BASE_URL).then(res => {
        this.listData = res.lists.map(d => {
          d.updateTimeStr = d.update_time ? utils.formatDate(new Date(1000 * (+d.update_time)), settings.datetime_format) : ''
          d.statusStr = d.status === 1 ? '已置顶' : '普通'
          d.userInfo = d.user_name + '（用户ID：' + d.user_id + '）'
          return d
        })
      }).catch(err => {
        console.warn(err)
      })
    },
    onPush ({ index, row }) {
      let url = BASE_URL + '/' + row.id
      utils.editData.call(this, url, { status: 1 }).then(response => {
        console.log(response)
        this.initList()
      }).catch(err => {
        console.warn(err)
      })
    },
    onDel ({ index, row }) {
      this.$confirm('即将删除当前评论, 是否继续?', row.title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = BASE_URL + '/' + row.id
        utils.deleteData.call(this, url).then(response => {
          console.log(response)
          this.initList()
        }).catch(err => {
          console.warn(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDialogOpen ({ mode }) {},
    addRow () {},
    addRowWithNewTemplate () {},
    handleRowAdd (row, done) {},
    handleDialogCancel (done) {}
  }
}
</script>
